//
//  TDOrderDetailC.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/18.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDOrderDetailC.h"

#import "BONoticeBar.h"

#import "TDOrderDetailScrollCell.h"
#import "TDOrderDetailGetBackTimeCell.h"
#import "TDCarDetailStoreAddressCell.h"
#import "TDOrderDetailTDStatusCell.h"
#import "TDOrderDetailBasicCell.h"
#import "TDOrderRepayView.h"

#import "TDEnsurePriceStatusC.h"
#import "TDCarDetailDescC.h"
#import "UILabel+height.h"
#import "NetWorkRequest.h"
#import "YXDateHelpObject.h"

#import "TDCheckNewPayC.h"
@interface TDOrderDetailC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UIView *nav_lineView;
@property (weak, nonatomic) IBOutlet UILabel *nav_title;

@property (weak, nonatomic) IBOutlet UIImageView *nav_leftImv;

@property (nonatomic,strong)NSArray *basicModelArray;//基础数据数组-----------
@property (nonatomic,strong)TDOrderDetailModel *orderDetailModel;
@end

@implementation TDOrderDetailC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}


- (void)setupUI {
    //设置导航条
    [self setupNavItem:YES];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];

    [self.tableView registerNib:[UINib nibWithNibName:@"TDOrderDetailScrollCell" bundle:nil] forCellReuseIdentifier:@"TDOrderDetailScrollCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDOrderDetailGetBackTimeCell" bundle:nil] forCellReuseIdentifier:@"TDOrderDetailGetBackTimeCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDCarDetailStoreAddressCell" bundle:nil] forCellReuseIdentifier:@"TDCarDetailStoreAddressCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDOrderDetailTDStatusCell" bundle:nil] forCellReuseIdentifier:@"TDOrderDetailTDStatusCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDOrderDetailBasicCell" bundle:nil] forCellReuseIdentifier:@"TDOrderDetailBasicCellId"];
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
}


- (void)setupNavItem:(BOOL)isDefault {
    if (isDefault) {
        self.nav_leftImv.image = [UIImage imageNamed:@"返回"];
        self.navView.backgroundColor = [UIColor clearColor];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
        self.nav_lineView.hidden = YES;
        self.nav_title.text = @"";
    }else {
        self.nav_leftImv.image = [UIImage imageNamed:@"返回选中"];
        self.navView.backgroundColor = [UIColor whiteColor];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
         self.nav_title.text = [NSString stringWithFormat:@"%@ %@",self.orderDetailModel.brandnm,self.orderDetailModel.seriesnm];
        self.nav_lineView.hidden = NO;
    }
}

// MARK: - 返回点击事件--------
- (IBAction)navBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4+self.basicModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 394;
    }else if (indexPath.row == 1) {
        return 117;
    }else if (indexPath.row == 2) {
        return 326;
    }
    else if (indexPath.row == 3) {
        return 106;
    }
    return 70;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TDOrderDetailScrollCellId";
    if (indexPath.row == 0) {
        iden = @"TDOrderDetailScrollCellId";
    }else if(indexPath.row == 1) {
        iden = @"TDOrderDetailGetBackTimeCellId";
    }else if (indexPath.row == 2) {
        iden = @"TDCarDetailStoreAddressCellId";
    }else if (indexPath.row == 3) {
        iden = @"TDOrderDetailTDStatusCellId";
    }else {
        iden = @"TDOrderDetailBasicCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        TDOrderDetailScrollCell *crderDetailScrollCell = (TDOrderDetailScrollCell *)cell;
        crderDetailScrollCell.orderDetailModel = self.orderDetailModel;
    }else if (indexPath.row == 1) {
        TDOrderDetailGetBackTimeCell *orderDetailGetBackTimeCell = (TDOrderDetailGetBackTimeCell *)cell;
        orderDetailGetBackTimeCell.lbe_startDate.text = self.orderDetailModel.startDate;
        orderDetailGetBackTimeCell.lbe_endDate.text = self.orderDetailModel.endDate;
        orderDetailGetBackTimeCell.lbe_endTimeHour.text = self.orderDetailModel.endTimeHour;
        orderDetailGetBackTimeCell.lbe_startTimeHour.text = self.orderDetailModel.startTimeHour;
    }else if (indexPath.row == 2) {
        TDCarDetailStoreAddressCell *carDetailStoreAddressCell = (TDCarDetailStoreAddressCell *)cell;
        carDetailStoreAddressCell.lbe_address.text = self.orderDetailModel.returnstorename;
        NSString *urlStr = [NSString stringWithFormat:@"http://api.map.baidu.com/staticimage?width=335&height=220&center=%@,%@&zoom=13&markers=%@,%@&markerStyles=0xBF0021",_orderDetailModel.pstorelongitude,_orderDetailModel.pstorelatitude,_orderDetailModel.pstorelongitude,_orderDetailModel.pstorelatitude];
        [carDetailStoreAddressCell.imv_storeAddress sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"car"]];
    }else if (indexPath.row == 3) {
        TDOrderDetailTDStatusCell *carStatusCell = (TDOrderDetailTDStatusCell *)cell;
        carStatusCell.lbe_status.text = self.orderDetailModel.statusnm;
        NSArray *tmpArray = _orderDetailModel.materiallist;
        TDOrderdetaillistEnumModel *enumModel = tmpArray.firstObject;
        NSString *tmpImageUrl = [NSString stringWithFormat:@"http://121.40.92.131/deepdriving/%@",enumModel.linkurl];
        [carStatusCell.imv_car sd_setImageWithURL:[NSURL URLWithString:tmpImageUrl] placeholderImage:[UIImage imageNamed:@"car"]];
    }else {
        TDOrderDetailBasicCell *carDescCell = (TDOrderDetailBasicCell *)cell;
        TDOrderDetailBasicModel *basicModel = self.basicModelArray[indexPath.row-4];
        carDescCell.lbe_titleDes.text = basicModel.titleDesc;
        carDescCell.lbe_titleDetail.text = basicModel.titleDetail;
        if (indexPath.row == 4) {
            carDescCell.lbe_titleDetail.text = _orderDetailModel.totalamount;
        }else if (indexPath.row == 5) {
            carDescCell.lbe_titleDetail.text = _orderDetailModel.pickupcode;
        }else if (indexPath.row == 6) {
            carDescCell.lbe_titleDetail.text = @"查看";
        }
        else if (indexPath.row == 7) {
            carDescCell.lbe_titleDetail.text = @"暂无";
            carDescCell.lbe_titleDetail.textColor = [UIColor lightGrayColor];
        }
    }
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 6) {
        TDEnsurePriceStatusC *ensurePriceStatusC = [[TDEnsurePriceStatusC alloc] init];
        ensurePriceStatusC.orderDetailModel = self.orderDetailModel;
        [self.navigationController pushViewController:ensurePriceStatusC animated:YES];
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if ([self.orderDetailModel.status isEqualToString:@"1"]) {
        TDOrderRepayView *rePayView = [TDOrderRepayView orderRepayView];
        rePayView.frame = CGRectMake(0, 0, Width, 70);
        __weak typeof(self) weakSelf = self;
        rePayView.orderDetailRepayBtnClickBlock = ^{
            TDCheckNewPayC *checkPayC = [[TDCheckNewPayC alloc] init];
            checkPayC.orderDetailModel = self.orderDetailModel;
            [weakSelf.navigationController pushViewController:checkPayC animated:YES];
            
        };
        return rePayView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if ([self.orderDetailModel.status isEqualToString:@"1"]) {
        return 70;
    }
    return 0.1;
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offY = self.tableView.contentOffset.y;
    NSLog(@"%.2f",offY);
    if (self.tableView.contentOffset.y > 220) {
        [self setupNavItem:NO];
    } else {
        [self setupNavItem:YES];
    }
}


// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {

    NSArray *tmpArray = @[@{@"titleDesc":@"试驾费用",@"titleDetail":@""},
                             @{@"titleDesc":@"提车码",@"titleDetail":@""},
                             @{@"titleDesc":@"保证金状态",@"titleDetail":@"查看"},
                             @{@"titleDesc":@"验车清单",@"titleDetail":@"暂无"}];
    self.basicModelArray = [NSArray yy_modelArrayWithClass:[TDOrderDetailBasicModel class] json:tmpArray];
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    NSDictionary *dic = @{
                          @"orderid":_orderListModel.orderid,
                          };
    __weak typeof(self) weakSelf = self;
    [NetWorkRequest postNetRequestData:OrderGetDetailM parmeters:dic response:^(NSDictionary *response) {
        [weakSelf progressHUDHideImmediately];
        if (response) {
            weakSelf.orderDetailModel = [TDOrderDetailModel yy_modelWithJSON:response[@"data"]];
            //根据日期来返回
            weakSelf.orderDetailModel.startTimeHour = [[YXDateHelpObject manager] getStrDateFromStrDate:weakSelf.orderDetailModel.planpickuptime OldFormat:@"yyyy-MM-dd HH:mm:ss" ByNewFormat:@"HH:mm"];
            weakSelf.orderDetailModel.endTimeHour = [[YXDateHelpObject manager] getStrDateFromStrDate:weakSelf.orderDetailModel.planreturntime OldFormat:@"yyyy-MM-dd HH:mm:ss" ByNewFormat:@"HH:mm"];
            
            weakSelf.orderDetailModel.startDate = [[YXDateHelpObject manager] getStrDateFromStrDate:weakSelf.orderDetailModel.planpickuptime OldFormat:@"yyyy-MM-dd HH:mm:ss" ByNewFormat:@"MM月dd日 EEEE"];
            weakSelf.orderDetailModel.endDate = [[YXDateHelpObject manager] getStrDateFromStrDate:weakSelf.orderDetailModel.planreturntime OldFormat:@"yyyy-MM-dd HH:mm:ss" ByNewFormat:@"MM月dd日 EEEE"];
            [weakSelf.tableView reloadData];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"获取订单列表失败" isSucceed:NO];
    }];
}



@end
